package com.co.video.controller;

import com.co.common.entity.Result;
import com.co.video.pojo.Video;
import com.co.video.service.VideoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@RestController
@RequestMapping("/video")

public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * TvVideo分页条件搜索实现
     *
     * @param video
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Video>> findPage(@RequestBody(required = false) Video video, @PathVariable int page, @PathVariable int size) {
        //调用TvVideoService实现分页条件查询TvVideo
        PageInfo<Video> pageInfo = videoService.findPage(video, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * TvVideo分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Video>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用TvVideoService实现分页查询TvVideo
        PageInfo<Video> pageInfo = videoService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param video
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Video>> findList(@RequestBody(required = false) Video video) {
        //调用TvVideoService实现条件查询TvVideo
        List<Video> list = videoService.findList(video);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用TvVideoService实现根据主键删除
        videoService.delete(id);
        return Result.ok();
    }

    /**
     * 修改TvVideo数据
     *
     * @param video
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@RequestBody Video video, @PathVariable String id) {
        //设置主键值
        video.setCid(id);
        //调用TvVideoService实现修改TvVideo
        videoService.update(video);
        return Result.ok();
    }

    /**
     * 新增TvVideo数据
     *
     * @param video
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody Video video) {
        //调用TvVideoService实现添加TvVideo
        videoService.add(video);
        return Result.ok();
    }

    /**
     * 根据ID查询TvVideo数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Video> findById(@PathVariable String id) {
        //调用TvVideoService实现根据主键查询TvVideo
        Video video = videoService.findById(id);
        return Result.ok(video);
    }

    /**
     * 查询TvVideo全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Video>> findAll() {
        //调用TvVideoService实现查询所有TvVideo
        List<Video> list = videoService.findAll();
        return Result.ok(list);
    }

    @GetMapping("/findcid/{aid}")
    public Result<List<Video>> findCidByAid(@PathVariable String aid) {
        List<Video> list = videoService.findCidByAid(aid);
        return Result.ok(list);
    }
}
